package data;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;

public class OrderDepartment {
	private String mOrderReceiver;
	private String mContactPhone;
	private Date mDate1;
	private Date mDate2;
	private String mHour;
	private String mComments;
	private List<OrderElement> mOrderElements;
	
	public OrderDepartment(String orderReceiver, String contactPhone, Date date1, Date date2, String hours, String comments, List<OrderElement> orderElements) {
		mOrderReceiver = orderReceiver;
		mContactPhone = contactPhone;
		mDate1 = date1;
		mDate2 = date2;
		mHour = hours;
		mComments = comments;
		mOrderElements = orderElements;
	}

	public String getOrderReceiver() {
		return mOrderReceiver;
	}

	public String getContactPhone() {
		return mContactPhone;
	}

	public Date getDate1() {
		return mDate1;
	}

	public Date getDate2() {
		return mDate2;
	}

	public String getHour() {
		return mHour;
	}

	public String getComments() {
		return mComments;
	}

	public List<OrderElement> getOrderElements() {
		return mOrderElements;
	}
	
	public static final JsonDeserializer<OrderDepartment> deserializer = new JsonDeserializer<OrderDepartment>() {

		@Override
		public OrderDepartment deserialize(JsonElement json, Type arg1,
				JsonDeserializationContext context) throws JsonParseException {
			String orderReceiver = json.getAsJsonObject().get("orderReceiver").isJsonNull() ? null : json.getAsJsonObject().get("orderReceiver").getAsString();
			String contactPhone = json.getAsJsonObject().get("contactPhone").isJsonNull() ? null : json.getAsJsonObject().get("contactPhone").getAsString();
			String dateStr = json.getAsJsonObject().get("date1").isJsonNull() ? null : json.getAsJsonObject().get("date1").getAsString();
			Date date1 = null;
			if (dateStr != null) {
				try {
					date1 = new SimpleDateFormat("yyyy-MM-DD").parse(dateStr);
				} catch (ParseException e) {
					date1 = null;
				}
			}
			dateStr = json.getAsJsonObject().get("date2").isJsonNull() ? null : json.getAsJsonObject().get("date2").getAsString();
			Date date2 = null;
			if (dateStr != null) {
				try {
					date2 = new SimpleDateFormat("yyyy-MM-DD").parse(dateStr);
				} catch (ParseException e) {
					date2 = null;
				}
			}
			String hours = json.getAsJsonObject().get("hour").isJsonNull() ? null : json.getAsJsonObject().get("hour").getAsString();
			String comments = json.getAsJsonObject().get("comments").isJsonNull() ? null : json.getAsJsonObject().get("comments").getAsString();
			List<OrderElement> orderElements = new ArrayList<>();
			for (JsonElement jsonOrderElement : json.getAsJsonObject().get("orderElements").getAsJsonArray()) {
				orderElements.add((OrderElement)context.deserialize(jsonOrderElement, OrderElement.class));
			}
			return new OrderDepartment(orderReceiver, contactPhone, date1, date2, hours, comments, orderElements);
		}
	};
	
	public static final JsonSerializer<OrderDepartment> serializer = new JsonSerializer<OrderDepartment>() {

		@Override
		public JsonElement serialize(OrderDepartment arg0, Type arg1,
				JsonSerializationContext arg2) {
			JsonObject obj = new JsonObject();
			obj.addProperty("mOrderReceiver", arg0.getOrderReceiver());
			obj.addProperty("mContactPhone", arg0.getContactPhone());
			obj.addProperty("mDate1", new SimpleDateFormat("yyyy-MM-dd").format(arg0.getDate1()));
			obj.addProperty("mDate2", new SimpleDateFormat("yyyy-MM-dd").format(arg0.getDate2()));
			obj.addProperty("mHour", arg0.getHour());
			obj.addProperty("mComments", arg0.getComments());
			obj.add("mOrderElements", arg2.serialize(arg0.getOrderElements(), new TypeToken<List<OrderElement>>() {
				}.getType()));
			return obj;
		}
	};
}
